package com.yangfan.springcloud.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter, Ordered {


    /**
     * 自定义的 拦截器
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //获取请求参数key 如果 key是空的 则返回错误页面
//        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
//        if(uname == null){
//            log.info("非法的请求参数");
//            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);//设置返回的状态码
//            return exchange.getResponse().setComplete();
//        }
        return chain.filter(exchange);//放行
    }


    /**
     * 加载这个过滤器的顺序，一般数值越小优先级越高
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }



}
